package org.streets.workflow.engine.mem;

import java.util.ArrayList;
import java.util.List;

import org.streets.workflow.engine.ILoop;
import org.streets.workflow.engine.INode;
import org.streets.workflow.engine.ITransition;
import org.streets.workflow.engine.WorkflowEngine;
import org.streets.workflow.engine.WorkflowException;
import org.streets.workflow.engine.event.INodeEventListener;
import org.streets.workflow.engine.event.NodeEvent;
import org.streets.workflow.engine.plugin.IPlugable;

/**
 *
 */
public abstract class AbstractNode implements INode, IPlugable {

	protected transient List<ITransition> leavingTransitionInstances = new ArrayList<ITransition>();
	protected transient List<ITransition> enteringTransitionInstances = new ArrayList<ITransition>();

    protected transient List<ILoop> leavingLoopInstances = new ArrayList<ILoop>();
    protected transient List<ILoop> enteringLoopInstances = new ArrayList<ILoop>();
    
    protected transient List<INodeEventListener> eventListeners = new ArrayList<INodeEventListener>();
    
    protected NodeType type = null;
	/* (non-Javadoc)
	 * @see org.fireflow.kenel.INodeInstance#addLeavingTransitionInstance(org.fireflow.kenel.ITransitionInstance)
	 */
    public NodeType getNodeType() {
        return type;
    }
    
	public void addLeavingTransition(ITransition transitionInstance) {
		leavingTransitionInstances.add(transitionInstance);
	}

	public List<ITransition> getLeavingTransitions(){
		return this.leavingTransitionInstances;
	}

	public List<ITransition> getEnteringTransitions(){
		return this.enteringTransitionInstances;
	}
	public void addEnteringTransition(ITransition transitionInstance){
		this.enteringTransitionInstances.add(transitionInstance);
	}

    public void addLeavingLoop(ILoop loopInstance) {
        leavingLoopInstances.add(loopInstance);

    }

    public List<ILoop> getLeavingLoops() {
        return this.leavingLoopInstances;
    }

    public List<ILoop> getEnteringLoops() {
        return this.enteringLoopInstances;
    }

    public void addEnteringLoop(ILoop loopInstance) {
        this.enteringLoopInstances.add(loopInstance);
    }
	
	public List<INodeEventListener> getEventListeners(){
		return eventListeners;
	}		
	/**
	 * wangmj  20090914 增加统一的触发方法，实现类中根据事件的不同而进行触发
	 * @param event
	 * @throws KernelException
	 */
	public void fireNodeEvent(WorkflowEngine engine, NodeEvent event) throws WorkflowException {
		for (int i=0;i<this.eventListeners.size();i++){
			INodeEventListener listener = this.eventListeners.get(i);
			listener.onNodeEventFired(engine, event);
		}
	}
	
	public String toString() {
	    return "Node Instance ["+getNodeId()+"]";
	}
}
